package com.zhida.smartcontrol.pagectrl.devicedetail;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.jay.tools.adapter.MyFragmentAdapter;
import com.jay.tools.ui.TopBarView;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.ResUtil;
import com.tan.indicator.MagicIndicator;
import com.tan.indicator.ViewPagerHelper;
import com.tan.indicator.buildins.commonnavigator.CommonNavigator;
import com.tan.indicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import com.tan.indicator.buildins.commonnavigator.abs.IPagerIndicator;
import com.tan.indicator.buildins.commonnavigator.abs.IPagerTitleView;
import com.tan.indicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import com.tan.indicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import com.tan.indicator.ext.titles.ScaleTransitionPagerTitleView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.pagectrl.devicedetail.autotime.AutoTimeFragment;
import com.zhida.smartcontrol.pagectrl.devicedetail.holiday.HolidayFragment;
import com.zhida.smartcontrol.pagectrl.devicedetail.newdata.NewDataFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.zonemanager.ZoneManagerFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * author: tanjianpei
 * date: 2019/10/17 017 10:19
 * description: 区段管理，四通道
 */
public class DeviceDetailFragment extends MVPBaseFragment<DeviceDetailContract.View, DeviceDetailPresenter> implements DeviceDetailContract.View {
    public static final int CHANNEL_4 = 1; // 四通道
    public static final int CHANNEL_8 = 2; // 八通道

    public static DeviceDetailFragment newInstance(String id, String code, int channel, String switchType) {
        Bundle args = new Bundle();
        args.putString("id", id);
        args.putString("code", code);
        args.putInt("channel", channel);
        args.putString("switchType", switchType);
        DeviceDetailFragment fragment = new DeviceDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @BindView(R.id.top)
    TopBarView mTopView;
    @BindView(R.id.indicator)
    MagicIndicator mIndicator;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_device_detail;
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        inflateTopBar(mTopView);
        Bundle bundle = getArguments();
        mPresenter.initTabData(bundle.getInt("channel"));

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(AutoTimeFragment.newInstance(bundle.getString("id")));
        fragmentList.add(HolidayFragment.newInstance(bundle.getString("id")));
        fragmentList.add(NewDataFragment.newInstance(bundle.getString("code"), bundle.getString("switchType")));

        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(new MyFragmentAdapter(getChildFragmentManager(), fragmentList));
    }

    @Override
    public void bindTab(List<String> tabList) {
        magic3(tabList);
    }

    private void magic3(final List<String> titles) {
        final int lineHeight = DimenUtils.dp2px(getProxyActivity(), 2);
        final int blue = ResUtil.getColor(getProxyActivity(), R.color.c_3862FF);
        final int gray = ResUtil.getColor(getProxyActivity(), R.color.c_666666);

        final CommonNavigator commonNavigator2 = new CommonNavigator(getProxyActivity());
        commonNavigator2.setAdjustMode(true);
        commonNavigator2.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView tvTitle = new ScaleTransitionPagerTitleView(context);
                tvTitle.setText(titles.get(index));
                tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                tvTitle.setNormalColor(gray);
                tvTitle.setSelectedColor(blue);
                tvTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                        if(index==2) {
                            NewDataFragment fragment = (NewDataFragment) getChildFragmentManager().getFragments().get(index);
                            fragment.requestData();
                        }

                    }
                });
                return tvTitle;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setLineHeight(lineHeight);
                indicator.setColors(blue);
                return indicator;
            }

        });
        mIndicator.setNavigator(commonNavigator2);
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }


}
